#!/usr/bin/perl -w

#####################################################################
# Program Variables - Go see the central config.pm!
#####################################################################
use FindBin qw($Bin);
use lib "$Bin/..";
use config;

#####################################################################
# Program Modules
#####################################################################
use DBI;

#####################################################################
# Program Start
#####################################################################

# Open Database
my $dbh = DBI->connect("dbi:SQLite:dbname=$database_file", "", "");

# Get the list of all the database tables
my @tablelist = @{ $dbh->selectcol_arrayref("SELECT name FROM sqlite_master WHERE type='table'") };

print "/*\n";
print "AquaStats Database Dump\n";
print "*/\n";

foreach $table (@tablelist) {
    print "-- -=-=-=- $table -=-=-=-\n";

    my $test_sth = $dbh->prepare("SELECT * FROM $table");
    $test_sth->execute();
    while (my $testref = $test_sth->fetchrow_hashref()) {
        my %testhash = %{$testref};

        my @column_names;
        my @column_data;

        print "INSERT INTO \"$table\" (";

        foreach $column (keys %testhash) {
            push(@column_names, "\"$column\"");
            my $data = $testhash{$column};

            #			$data =~ s/\n/\\n/gm;
            #			$data =~ s/\r/\\r/gm;
            $data =~ s/'/''/gm;

            push(@column_data, "\'$data\'");
        }

        print join(", ", @column_names), ") VALUES (";
        print join(", ", @column_data),  ");\n";

    }

    print "-- \n";
}

exit 0;

